#!/usr/bin/env python
# _*_coding:utf-8_*_

"""
自定义排序
@Time : 2022/2/18 17:06
@Author: RunAtWorld
@File: using_sort.py
"""


def customer_tuple_sort_func():
    L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]

    def by_name(t):
        return t[0].lower()

    def by_score(t):
        # 默认排列顺序为从低到高t[1]， 要使其从高到低，将分数先取负数-t[1]即可
        return -t[1]

    L2 = sorted(L, key=by_name)
    print(L2)
    L2 = sorted(L, key=by_score)
    print(L2)


def sort_arr():
    L = [2, 3, 4, 5, 6, 8, 21]
    L.sort(key=lambda x: (-x, x))
    print(L)


def customer_tuple_sort():
    L = [(12, 12), (34, 13), (32, 15), (12, 24), (32, 64), (32, 11)]
    L.sort(key=lambda x: (x[0], -x[1]))
    print(L)


def custom_sort_tuple_desc():
    L = [(12, 12, 22), (34, 13, 43), (32, 15, 22), (12, 24, 17), (32, 64, 98), (32, 11, 34), (32, 11, 4)]
    L.sort(key=lambda x: (x[0], x[1], -x[2]))
    for i, j, k in L:
        print(i, j, k)


def custom_sorted_tuple_desc():
    L = [(12, 12, 22), (34, 13, 43), (32, 15, 22), (12, 24, 17), (32, 64, 98), (32, 11, 34), (32, 11, 4)]
    L1 = sorted(L, key=lambda x: (x[0], x[1], -x[2]))
    for i, j, k in L1:
        print(i, j, k)


class Student:
    def __init__(self, name, age, height, chinese, english, math):
        self.name = name
        self.age = age
        self.height = height
        self.chinese = chinese
        self.english = english
        self.math = math

    def __str__(self):
        return "{0:10}{1:6}{2:6}{3:6}{4:6}" \
            .format(self.name, self.height, self.chinese, self.english, self.math)


def custom_sort_student():
    stus = [
        Student('zhangfei', 33, 181, 81, 72, 72),
        Student('guanyu', 33, 181, 71, 60, 64),
        Student('liubei', 34, 171, 60, 80, 81),
        Student('zhaozilong', 33, 181, 84, 78, 89),
        Student('zhouyu', 33, 181, 84, 72, 72),
        Student('sunquan', 33, 181, 84, 78, 89),
        Student('mayun', 33, 191, 51, 70, 44),
        Student('zhugeliang', 33, 181, 84, 78, 72),
    ]
    stus.sort(key=lambda x: (x.math, x.english, -x.chinese))
    print("{0:10}{1:6}{2:6}{3:6}{4:6}".format('name', 'height', 'chinese', 'english', 'math'))
    for i in stus:
        print(i)


def cumstomer_dict_sort():
    arr = [
        {'name': 'zhangfei', 'height': 181, 'chinese': 81, 'english': 72, 'math': 72},
        {'name': 'zhouyu', 'height': 181, 'chinese': 84, 'english': 72, 'math': 72},
        {'name': 'zhugeliang', 'height': 181, 'chinese': 84, 'english': 78, 'math': 72},
        {'name': 'zhaozilong', 'height': 181, 'chinese': 84, 'english': 78, 'math': 89},
        {'name': 'liubei', 'height': 171, 'chinese': 60, 'english': 80, 'math': 81},
        {'name': 'sunquan', 'height': 181, 'chinese': 84, 'english': 78, 'math': 89},
        {'name': 'guanyu', 'height': 181, 'chinese': 71, 'english': 60, 'math': 64},
        {'name': 'mayun', 'height': 191, 'chinese': 51, 'english': 70, 'math': 44}
    ]
    arr.sort(key=lambda x: (x['math'], x['english'], -x['chinese']))
    print("{0:10}{1:6}{2:6}{3:6}{4:6}".format('name', 'height', 'chinese', 'english', 'math'))
    for k in arr:
        print("{0:10}{1:6}{2:6}{3:6}{4:6}".format(k['name'], k['height'], k['chinese'], k['english'], k['math']))


if __name__ == '__main__':
    customer_tuple_sort_func()
    customer_tuple_sort()
    custom_sort_tuple_desc()
    cumstomer_dict_sort()
    print("==custom_sort_student===")
    custom_sort_student()

    custom_sorted_tuple_desc()

    sort_arr()

